import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../layout/Home.vue'
import Dash from '../views/Dashboard/Index.vue'
import local from '../utils/local.js'

Vue.use(VueRouter)
const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  // 主页面
  {
    path: '/home',
    name: 'Home',
    component: Home,
    redirect: '/dashboard',
    children: [
      // 后台首页
      { path: '/dashboard', component: Dash }
    ]
  },
  //  登录
  {
    path: '/login',
    component: () =>
      import(/* webpackChunkName: "Login" */ '../views/Login.vue')
  },
  {
    path: '*',
    redirect: '/404'
  },
  // 404页面
  {
    path: '/404',
    component: () =>
      import(/* webpackChunkName: "Login" */ '../views/Error/Index.vue')
  }
]
// component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
const router = new VueRouter({
  routes
})
// 动态路由
const SyncRouter = {
  path: '/home',
  component: Home,
  redirect: '/dashboard',
  children: [
    // 后台首页
    {
      path: '/dashboard',
      meta: { name: '后台首页', icon: 'icon-shouye' },
      component: Dash,
      isVisible: true
    },
    //  订单管理
    {
      path: '/order',
      meta: { name: '订单管理', role: ['super', 'normal'], icon: 'icon-icon-' },
      isVisible: true,
      redirect: '/order/list',
      component: () =>
        import(/* webpackChunkName: "order" */ '../views/Order/Index.vue'),
      children: [
        {
          path: '/order/list',
          meta: { name: '订单列表' },
          isVisible: true,
          component: () =>
            import(/* webpackChunkName: "order" */ '../views/Order/List.vue')
        },
        {
          path: '/order/edit',
          meta: { name: '订单编辑', role: ['super'] },
          isVisible: false,
          component: () =>
            import(/* webpackChunkName: "order" */ '../views/Order/Edit.vue')
        }
      ]
    },
    // 商品管理
    {
      path: '/goods',
      meta: { name: '商品管理', icon: 'icon-shangpin' },
      isVisible: true,
      component: () =>
        import(/* webpackChunkName: "Login" */ '../views/Goods/Index.vue'),
      children: [
        {
          path: '/goods/list',
          meta: { name: '商品列表' },
          isVisible: true,
          component: () =>
            import(/* webpackChunkName: "Login" */ '../views/Goods/List.vue')
        },
        {
          path: '/goods/cate',
          meta: { name: '商品分类' },
          isVisible: true,
          component: () =>
            import(/* webpackChunkName: "Login" */ '../views/Goods/Cate.vue')
        },
        {
          path: '/goods/add',
          component: () =>
            import(/* webpackChunkName: "Login" */ '../views/Goods/Add.vue'),
          meta: { name: '添加商品' },
          isVisible: true
        }
      ]
    },
    // 店铺管理
    {
      path: '/shop',
      meta: { name: '店铺管理', role: ['super'] },
      isVisible: true,
      component: () =>
        import(/* webpackChunkName: "Login" */ '../views/Shop/Index.vue')
    },
    // 账号管理
    {
      path: '/account',
      meta: {
        name: '账号管理',
        role: ['super', 'normal'],
        icon: 'icon-zhanghao'
      },
      isVisible: true,
      component: () =>
        import(/* webpackChunkName: "Login" */ '../views/Account/Index.vue'),

      children: [
        {
          path: '/account/add',
          component: () =>
            import(/* webpackChunkName: "Login" */ '../views/Account/Add.vue'),
          meta: { name: '添加账号', role: ['super'] },
          isVisible: true
        },
        {
          path: '/account/list',
          component: () =>
            import(/* webpackChunkName: "Login" */ '../views/Account/List.vue'),
          meta: { name: '账号列表', role: ['super'] },
          isVisible: true
        },
        {
          path: '/account/reset',
          component: () =>
            import(
              /* webpackChunkName: "Login" */ '../views/Account/Reset.vue'
            ),
          meta: { name: '修改账户' },
          isVisible: true
        },
        {
          path: '/account/personal',
          component: () =>
            import(
              /* webpackChunkName: "Login" */ '../views/Account/Personal.vue'
            ),
          meta: { name: '个人中心' },
          isVisible: false
        }
      ]
    },
    // 销售统计
    {
      path: '/total',
      meta: { name: '销售统计', role: ['super'], icon: 'icon-tongji' },
      isVisible: true,
      component: () =>
        import(/* webpackChunkName: "Login" */ '../views/Total/Index.vue'),

      children: [
        {
          path: '/total/commodity',
          component: () =>
            import(
              /* webpackChunkName: "Login" */ '../views/Total/Commodity.vue'
            ),
          meta: { name: '商品统计' },
          isVisible: true
        },
        {
          path: '/total/received',
          component: () =>
            import(
              /* webpackChunkName: "Login" */ '../views/Total/Received.vue'
            ),
          meta: { name: '订单统计' },
          isVisible: true
        }
      ]
    }
  ]
}
// 2.0 声明一个函数，对上面分离的路由，进行计算
calcRouter()
export function calcRouter() {
  // 2.1 获取到最终过滤出来的路由
  const role = local.get('role')
  if (!role) return
  const finallyRoter = filterFn(SyncRouter.children, role)
  // console.log(finallyRoter, 112)
  // 2.2 合并到主路由上
  SyncRouter.children = finallyRoter
  router.addRoutes([SyncRouter])
  const NavMenu = caclMenu(finallyRoter)
  local.set('showMenu', JSON.stringify(NavMenu))
}

function caclMenu(menu) {
  return menu.filter(item => {
    if (item.isVisible) {
      if (item.children && item.children.length > 0) {
        item.children = caclMenu(item.children)
      }
      return true
    } else {
      return false
    }
  })
}

// 3.0 主要做功能的函数
function filterFn(router, role) {
  return router.filter(item => {
    if (haspermission(item, role)) {
      if (item.children) {
        item.children = filterFn(item.children, role)
      }
      return true
    } else {
      return false
    }
  })
}
// 4.0 鉴权功能
function haspermission(item, r) {
  if (item.meta && item.meta.role) {
    return item.meta.role.includes(r)
  } else {
    return true
  }
}

// 解决双击路由报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
// 设置导航守卫
router.beforeEach((to, from, next) => {
  const token = local.get('token')
  if (token) {
    next()
  } else {
    if (to.path === '/login') {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
